@(status: gitbucket.core.service.PullRequestService.MergeStatus,
  issue: gitbucket.core.model.Issue,
  pullreq: gitbucket.core.model.PullRequest,
  originRepository: gitbucket.core.service.RepositoryService.RepositoryInfo,
  forkedRepository: gitbucket.core.service.RepositoryService.RepositoryInfo)(implicit context: gitbucket.core.controller.Context)
@import gitbucket.core.service.SystemSettingsService
@import context._
@import gitbucket.core.view.helpers._
@import model.CommitState
<div class="box issue-comment-box" style="background-color: @if(status.hasProblem){ #fbeed5 }else{ #d8f5cd };">
  <div class="box-content issue-content" style="border: 1px solid @if(status.hasProblem){ #c09853 }else{ #95c97e };padding:0">
    <div id="merge-pull-request">
      @if(!status.statuses.isEmpty){
        <div class="build-statuses">
          @defining(status.commitStateSummary){ case (summaryState, summary) =>
            <div class="build-status-item-header">
              <a class="pull-right" id="toggle-all-checks"></a>
              <span class="build-status-icon text-@{summaryState.name}">@commitStateIcon(summaryState)</span>
              <strong class="text-@{summaryState.name}">@commitStateText(summaryState, pullreq.commitIdTo)</strong>
              <span class="text-@{summaryState.name}">— @summary checks</span>
            </div>
          }
          <div class="build-statuses-list" style="@if(status.isAllSuccess){ display:none; }else{ }">
            @status.statusesAndRequired.map{ case (status, required) =>
            <div class="build-status-item">
              <div class="pull-right">
                @if(required){ <span class="label">Required</span> }
                @status.targetUrl.map{ url => <a href="@url">Details</a> }
              </div>
              <span class="build-status-icon text-@{status.state.name}">@commitStateIcon(status.state)</span>
              <strong>@status.context</strong>
              @status.description.map{ desc => <span class="muted">— @desc</span> }
            </div>
            }
          </div>
        </div>
      }
      <div style="padding:15px">
        @if(status.hasConflict){
          <div class="merge-indicator merge-indicator-alert"><span class="octicon octicon-alert"></span></div>
          <span class="strong">This branch has conflicts that must be resolved</span>
          <div class="small">
            @if(status.hasMergePermission){
              <a href="#" class="show-command-line">Use the command line</a> to resolve conflicts before continuing.
            } else {
              Only those with write access to this repository can merge pull requests.
            }
          </div>
        } else { @if(status.branchIsOutOfDate){
          @if(status.hasUpdatePermission){
            <div class="pull-right">
              <form method="POST" action="@url(originRepository)/pull/@pullreq.issueId/update_branch">
                <input type="hidden" name="expected_head_oid" value="@pullreq.commitIdFrom">
                <button class="btn"@if(!status.canUpdate){ disabled="true"} id="update-branch-button">Update branch</button>
              </form>
            </div>
          }
          <div class="merge-indicator merge-indicator-alert"><span class="octicon octicon-alert"></span></div>
          <span class="strong">This branch is out-of-date with the base branch</span>
          <div class="small">
            Merge the latest changes from <code>@pullreq.branch</code> into this branch.
          </div>
        } else { @if(status.hasRequiredStatusProblem) {
          <div class="merge-indicator merge-indicator-warning"><span class="octicon octicon-primitive-dot"></span></div>
          <span class="strong">Required statuses must pass before merging.</span>
          <div class="small">
            All required status checks on this pull request must run successfully to enable automatic merging.
          </div>
        } else {
          <div class="merge-indicator merge-indicator-success"><span class="octicon octicon-check"></span></div>
          @if(status.hasMergePermission){
            <span class="strong">Merging can be performed automatically.</span>
            <div class="small">
              Merging can be performed automatically.
            </div>
          } else {
            <span class="strong">This branch has no conflicts with the base branch.</span>
            <div class="small">
              Only those with write access to this repository can merge pull requests.
            </div>
          }
        } } }
      </div>
      @if(status.hasMergePermission){
        <div style="padding:15px;border-top:solid 1px #e5e5e5;background:#fafafa">
          <input type="button" class="btn btn-lg @if(!status.hasProblem){btn-success} else {btn-default}" id="merge-pull-request-button" value="Merge pull request"@if(!status.canMerge){ disabled="true"}/>
          &nbsp;&nbsp;You can also merge branches on the <a href="#" class="show-command-line">command line</a>.
          <div id="command-line" style="display: none;margin-top: 15px;">
            <hr />
            @if(status.hasConflict){
              <span class="strong">Checkout via command line</span>
              <p>
                If you cannot merge a pull request automatically here, you have the option of checking
                it out via command line to resolve conflicts and perform a manual merge.
              </p>
            } else {
              <span class="strong">Merging via command line</span>
              <p>
                If you do not want to use the merge button or an automatic merge cannot be performed,
                you can perform a manual merge on the command line.
              </p>
            }
            @helper.html.copy("repository-url-copy", forkedRepository.httpUrl){
              <div class="input-group-btn" data-toggle="buttons">
                <label class="btn btn-sm btn-default active" id="repository-url-http"><input type="radio" checked>HTTP</label>
                @if(settings.ssh && loginAccount.isDefined){
                  <label class="btn btn-sm btn-default" id="repository-url-ssh"><input type="radio">SSH</label>
                }
              </div>
              <input type="text" class="form-control input-sm" value="@forkedRepository.httpUrl" id="repository-url" readonly />
            }
            <div style="margin-top: 10px;">
              <p>
                <span class="strong">Step 1:</span> From your project repository, check out a new branch and test the changes.
              </p>
              @defining(s"git checkout -b ${pullreq.requestUserName}-${pullreq.requestBranch} ${pullreq.branch}\n" +
                        s"git pull ${forkedRepository.httpUrl} ${pullreq.requestBranch}"){ command =>
                @helper.html.copy("merge-command-copy-1", command, "position: absolute; right: 31px;")()
                <pre style="font-size: 12px; border-radius: 3px;" id="merge-command">@Html(command)</pre>
              }
            </div>
            <div>
              <p>
                <span class="strong">Step 2:</span> Merge the changes and update on the server.
              </p>
              @defining(s"git checkout ${pullreq.branch}\ngit merge --no-ff ${pullreq.requestUserName}-${pullreq.requestBranch}\n" +
                        s"git push origin ${pullreq.branch}"){ command =>
                @helper.html.copy("merge-command-copy-2", command, "position: absolute; right: 31px;")()
                <pre style="font-size: 12px; border-radius: 3px;">@command</pre>
              }
            </div>
          </div>
        </div>
      }
    </div>
    <div id="confirm-merge-form" style="display: none;">
      <form method="POST" action="@url(originRepository)/pull/@pullreq.issueId/merge">
        <div class="strong">
          Merge pull request #@issue.issueId from @{pullreq.requestUserName}/@{pullreq.requestBranch}
        </div>
        <span id="error-message" class="error"></span>
        <textarea name="message" style="height: 80px;" class="form-control">@issue.title</textarea>
        <div>
          <input type="button" class="btn" value="Cancel" id="cancel-merge-pull-request"/>
          <input type="submit" class="btn btn-success" value="Confirm merge"/>
        </div>
      </form>
    </div>
  </div>
</div>

<script>
$(function(){
  $('.show-command-line').click(function(){
    $('#command-line').toggle();
    return false;
  });
  function setToggleAllChecksLabel(){
    $("#toggle-all-checks").text($('.build-statuses-list').is(":visible") ? "Hide all checks" : "Show all checks");
  }
  setToggleAllChecksLabel();
  $('#toggle-all-checks').click(function(){
    $('.build-statuses-list').toggle();
    setToggleAllChecksLabel();
  })

  $('#merge-pull-request-button').click(function(){
    $('#merge-pull-request').hide();
    $('#confirm-merge-form').show();
  });

  @forkedRepository.sshUrl.map { sshUrl =>
    $('#repository-url-http').click(function(e){
      // Update URL box
      $('#repository-url').val('@forkedRepository.httpUrl');
      $('#repository-url-copy').attr('data-clipboard-text', $('#repository-url').val());
      // Update command guidance
      $('#merge-command').text($('#merge-command').text().replace(
        '@sshUrl', '@forkedRepository.httpUrl'
      ));
      $('#merge-command-copy-1').attr('data-clipboard-text', $('#merge-command').text());
    });

    $('#repository-url-ssh').click(function(e){
      // Update URL box
      $('#repository-url').val('@sshUrl');
      $('#repository-url-copy').attr('data-clipboard-text', $('#repository-url').val());
      // Update command guidance
      $('#merge-command').text($('#merge-command').text().replace(
        '@forkedRepository.httpUrl', '@sshUrl'
      ));
      $('#merge-command-copy-1').attr('data-clipboard-text', $('#merge-command').text());
    });
  }
});
</script>